package com.mingzhi.lotterysystem.controller;

import com.mingzhi.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.mingzhi.lotterysystem.common.exception.ControllerException;
import com.mingzhi.lotterysystem.common.pojo.CommonResult;
import com.mingzhi.lotterysystem.common.utils.JacksonUtil;
import com.mingzhi.lotterysystem.controller.param.CreatePrizeParam;
import com.mingzhi.lotterysystem.controller.param.PageParam;
import com.mingzhi.lotterysystem.controller.result.FindPrizeListResult;
import com.mingzhi.lotterysystem.service.PictureService;
import com.mingzhi.lotterysystem.service.PrizeService;
import com.mingzhi.lotterysystem.service.dto.PageListDTO;
import com.mingzhi.lotterysystem.service.dto.PrizeDTO;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

//Prize 奖品
@RestController
public class PrizeController {

    private static final Logger logger = LoggerFactory.getLogger(PrizeController.class);

    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeService prizeService;

    /**
     * 图片上传
     *
     * @param file
     * @return
     */
    @RequestMapping("/pic/upload")
    public String uploadPic(MultipartFile file) {
        logger.info("uploadPic file(原始文件名)：{}", file.getOriginalFilename());
        return pictureService.savePicture(file);
    }

    /**
     * 创建奖品
     *
     * @param param    从前端接受一个表单数据 要使用@RequestPart 注解
     * @param prizePic 图片
     * @return
     * @RequestPart 用于接受表单数据的 multipart/form-data
     * @RequestBody 用于接受json字符串
     */
    @RequestMapping("/prize/create")
    public CommonResult<Long> createPrize(@Valid @RequestPart("param") CreatePrizeParam param,
                                          @RequestPart("prizePic") MultipartFile prizePic) {
        logger.info("createPrize CreatePrizeParam：{}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(prizeService.createPrize(param, prizePic));
    }

    //参数这里没有定义@RequestBody
    //不想用json来进行前后端传输
    //直接把参数进行拼接传输
    @RequestMapping("/prize/find-list")
    public CommonResult<FindPrizeListResult> findPrizeList(PageParam param) {
        logger.info("findPrizeList PageParam：{}", JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> pageListDTO = prizeService.findPrizeList(param);
        return CommonResult.success(convertToFindPrizeListResult(pageListDTO));
    }

    private FindPrizeListResult convertToFindPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
        if(null == pageListDTO){
            throw new ControllerException(ControllerErrorCodeConstants.FIND_PRIZE_LIST_ERROR);
        }
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(pageListDTO.getTotal());
        result.setRecords(
                pageListDTO.getRecords().stream()
                        .map(prizeDTO -> {
                            FindPrizeListResult.PrizeInfo prizeInfo = new FindPrizeListResult.PrizeInfo();
                            prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                            prizeInfo.setPrizeName(prizeDTO.getName());
                            prizeInfo.setDescription(prizeDTO.getDescription());
                            prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                            prizeInfo.setPrice(prizeDTO.getPrice());
                            return prizeInfo;
                        }).collect(Collectors.toList())
        );
        return result;
    }
}
